package jm.nj.demo23main.b_c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class C3p0Demo {

    @Test
    public void demo() throws Exception{
        //创建池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql:///day23");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        //连接
        Connection conn = dataSource.getConnection();

        //操作
        PreparedStatement pstmt = conn.prepareStatement("select * from user");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt("uid") + "  "
                    + rs.getString("username") + "  "
                    + rs.getString("password"));
        }

        rs.close();
        pstmt.close();
        // 归还连接对象
        /**
         * 连接池对connection对象进行了增强，把其close()方法的作用，由原来的关闭连接 改成了 把连接归还到连接池
         */
        conn.close();
    }

    @Test
    public void demo2() throws SQLException {
        //创建池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        Connection conn = dataSource.getConnection();


        //操作
        PreparedStatement pstmt = conn.prepareStatement("select * from user");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt("uid") + "  " + rs.getString("username") + "  " + rs.getString("password"));
        }

        rs.close();
        pstmt.close();

        conn.close();
    }



    @Test
    public void demo3() throws Exception {
        // 创建连接池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource("/myconfig");

        // 从连接池中获取连接
        Connection conn = dataSource.getConnection();

        // 正常的JDBC操作
        PreparedStatement pstmt = conn.prepareStatement("select * from user");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt("uid") + "  " + rs.getString("username") + "  " + rs.getString("password"));
        }

        rs.close();
        pstmt.close();
        // 归还连接对象
        /**
         * 连接池对connection对象进行了增强，把其close()方法的作用，由原来的关闭连接 改成了 把连接归还到连接池
         */
        conn.close();

    }
    @Test
    public void demo05() throws Exception {
        // 创建连接池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource("myconfig");

        // 从连接池中获取连接
        Connection conn = dataSource.getConnection();

        // 正常的JDBC操作
        PreparedStatement pstmt = conn.prepareStatement("select * from user");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt("uid") + "  " + rs.getString("username") + "  " + rs.getString("password"));
        }

        rs.close();
        pstmt.close();
        // 归还连接对象
        /**
         * 连接池对connection对象进行了增强，把其close()方法的作用，由原来的关闭连接 改成了 把连接归还到连接池
         */
        conn.close();

    }

}
